/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class BoggleScore {
public:
    long long bestScore(vector <string> grid, vector <string> words);
};
const Int MOD = 10000000000000LL ;

inline Int domod(Int N, Int MOD) {
    if ( N < 0 ) {
        N+=MOD;
    }
    while ( N >= MOD ) {
        N-=MOD;
    }
    return N;
}

vector< string > g;
vector< string > w;
int rr[]={+0,+0,-1,+1, -1,-1, +1, +1};
int cc[]={-1,+1,+0,+0, -1,+1, +1, -1};
Int dp[5][5][52][52];
Int go( int r, int c, int id , int I ) {
    if ( id==LN(w[I]) ) {
        return 1LL;
    }
    Int &ways= dp[r][c][id][I];
    if ( ways!=-1 ) {
        return ways;
    }

    ways=0LL;
    for (int i=0;i<8;++i) {
        int tr = r+rr[i];
        int tc = c+cc[i];
        if ( tr < 0 || tr >= 4 || tc < 0 || tc >= 4 )continue;
        if ( g[tr][tc]== w[I][id] ) {
            Int v = go( tr, tc, id+1, I );
            ways = domod( (ways+v),MOD );
        }
    }
    return ways;
}

long long BoggleScore :: bestScore(vector <string> grid, vector <string> words) {
    long long retval=0;
    g.clear();
    w.clear();
    SET(dp,-1);
    g = grid;
    w = words;
    for (int i=0;i<SZ(w);++i ) {
        Int tmp=0LL;
        for (int m=0;m<4;++m) {
            for (int n=0;n<4;++n) {
                if ( g[m][n]!=w[i][0] )continue;

                tmp = domod( tmp + go( m,n,1,i ) , MOD );
            }
        }
        Int res=0;
        Int len=LN(w[i]);
        len*=len;
        res = domod( (len*tmp),MOD );
        retval = domod( retval+res, MOD);

        //  printf("%s = %lld\n",w[i].c_str(),tmp );
    }
    return retval;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <string> p0, vector <string> p1, bool hasAnswer, long long p2) {
    cout << "Test " << testNum << ": [" << "{";
    for (int i = 0; int(p0.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p0[i] << "\"";
    }
    cout << "}" << "," << "{";
    for (int i = 0; int(p1.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p1[i] << "\"";
    }
    cout << "}";
    cout << "]" << endl;
    BoggleScore *obj;
    long long answer;
    obj = new BoggleScore();
    clock_t startTime = clock();
    answer = obj->bestScore(p0, p1);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p2 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p2;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    vector <string> p0;
    vector <string> p1;
    long long p2;

    {
        // ----- test 0 -----
        string t0[] = {"XXEY","XXXX","XXXX","XXXX"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"EYE"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 9ll;
        all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        string t0[] = {"XEYE","XXXX","XXXX","XXXX"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"EYE"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 36ll;
        all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        string t0[] = {"TEXX","REXX","XXXX","XXXX"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"TREE"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 32ll;
        all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        string t0[] = {"XXXX","XSAX","XDNX","XXXX"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"SANDS","SAND","SAD","AND"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 59ll;
        all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        string t0[] = {"TREX","XXXX","XXXX","XXXX"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"TREE"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 0ll;
        all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING


//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
